/**
 * ffui 基础函数
 * 
 * @type
 */

jQuery.ffui = {
	/**
	 * 创建一个dom，如果已存在就选择这个dom ,并返回jq对象
	 * 
	 * @param {}
	 *            id 标签id
	 * @param {}
	 *            tag 标签名称
	 * @param {}
	 *            attr 属性json
	 * @return {} jq dom
	 */
	getId : function(id, tag, attr) {
		var $obj = $("#" + id);
		var attrStr = "";

		if ($obj.size() == 0) {
			if (attr) {
				$.each(attr, function(key, val) {
							attrStr += key + "=" + val + " ";
						});
			}
			if (!tag) {
				tag = "div";
			}
			$obj = $("<" + tag + " id='" + id + "' " + attrStr + " ></" + tag
					+ ">");
			$("body").append($obj);
		}
		return $obj;
	},
	/**
	 * 创建一个遮罩层
	 * 
	 * @return {遮罩层jq对象}
	 */
	mask : function(id, color) {
		if (!id) {
			id = "ffui-mask";
		}
		if (!color) {
			color = "#000000";
		}

		var $mask = $.ffui.getId(id);
		var $body = $("body");

		$mask.css({
					opacity : 0.50,
					filter : "alpha(opacity=50)",
					"background-color" : color,
					"z-index" : 198,
					position : "absolute",
					width : "100%",
					height : $(document).height(),
					top : 0,
					left : 0,
					display : "none"
				});
		return $mask;
	}
};
/**
 * 
 */
$.fn.extend({
			/**
			 * 在窗口中居中显示某对象
			 */
			centerShow : function() {
				var $obj = $(this);
				var width = $obj.outerWidth(true);
				var height = $obj.outerHeight(true);
				if ($.browser.msie && $.browser.version == "6.0") {
					$obj.css({
								"position" : "absolute",
								"display" : "block",
								visibility : "visible"
							});
					var ie6Style = function() {
						$obj.css({
									"left" : Math
											.floor(($(window).width() - width)
													/ 2),
									"top" : Math
											.floor(($(window).height() - height)
													/ 2)
											+ $(window).scrollTop()
								});
					};
					ie6Style();
					$(window).resize(function() {
								ie6Style();
							}).scroll(function() {
								ie6Style();
							});
				} else {
					var $centerBox = $("#ffui-center-box");
					
					if ($centerBox.size() == 0) {
						$centerBox = $("<div id='ffui-center-box' />");
						$centerBox.css({
									"position" : "fixed",
									"top" : "50%",
									"left" : "50%",
									"z-index" : 199
								});
						$("body").append($centerBox);
					}
					if ($obj.size() > 1) {
						$centerBox.find("#"+$obj.eq(0).attr("id")).remove();
					}
					$centerBox.append($obj);
					$obj.css({
								"position" : "absolute",
								"left" : -Math.floor(width / 2),
								"top" : -Math.floor(height / 2),
								"display" : "block",
								visibility : "visible"
							});
				}
			},
			/**
			 * 将对象固定在窗口中某个位置
			 * 
			 * @param {}
			 *            css
			 * @return {}
			 */
			posShow : function(css) {
				var obj = this;
				if ($.browser.msie && $.browser.version == "6.0") {
					$.extend(css, {
								"position" : "absolute",
								"display" : "block"
							});

					var tempCss = {};
					$.extend(tempCss, css);
					var height = $(obj).height();
					var setPos = function() {
						if (css["bottom"] != "undefined") {
							tempCss["bottom"] = parseInt(css["bottom"]);
							tempCss["zoom"] = 1;
							$(obj).css(tempCss);
							tempCss["zoom"] = 0;
						}
						$(obj).css(tempCss);
					};

					setPos();

					$(window).scroll(function() {
								setPos();
							}).resize(function() {
								setPos();
							});

				} else {
					$.extend(css, {
								"position" : "fixed",
								"display" : "block"
							});
					$(obj).css(css);
				}

				return this;
			},
			// 延迟关闭
			lazyHide : function(time) {
				var $obj = $(this);
				setTimeout(function() {
							$obj.fadeOut("slow");
						}, time);
				return this;
			},
			//文本框文字计数器
			wordCount : function(options) {
				var o = {
					tipId : "tip",
					max : 50,
					maxClass : "maxClass"

				};
				$.extend(o, options);
				var $obj = $(this);
				var $tip = $("#" + o.tipId);
				var countByteLength = function(str, cnCharByteLen) {
					var byteLen = 0;

					for (var i = 0; i < str.length; i++) {
						if (/[\x00-\xff]/.test(str.charAt(i))) {
							byteLen += 1;
						} else {
							byteLen += cnCharByteLen;
						}
					}
					// $("body").append(str + "===" + str.length + ",<br/>");
					return Math.ceil(byteLen / 2);
					// return byteLen;
				};

				var count = 0;
				// onpropertychange
				$obj.bind("keydown blur focus paste cut", function(event) {
							count = countByteLength($.trim($obj.val()), 2);

							$tip.html(count);

							if (count > o.max) {
								$tip.addClass(o.maxClass);
							} else if (count == o.max) {
								event.preventDefault();
							} else {
								$tip.removeClass(o.maxClass);
							}

						});
			}
		});
